package com.lntea.redis;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;

public class ListDemo {
	Jedis jedis;
	
	@Before
	public void connect(){
		jedis = new Jedis("192.168.1.102", 6379);
	}
	
	@Test
	public void llen(){
		//return length of list,if key is not exists,return 0
		Long len = jedis.llen("list2");
		System.out.println("list len: "+len);
	}
	
	@Test
	public void blpop(){
		//blocking lpop
		List<String> list = jedis.blpop(0, "list3","list4");
		System.out.println(list.toString());
	}
	
	@Test
	public void brpoplpush(){
		//blocking rpop source and lpush destination
		String result = jedis.brpoplpush("list3", "list4", 0);
		System.out.println("brpoplpush result: "+result);
	}
	
	@Test 
	public void lrange(){ 
		List<String> list = jedis.lrange("list", 0, jedis.llen("list"));
		System.out.println(list.toString());
	}
	
	@Test
	public void lpush(){
		//push value to the left of list
		//return index of value
		Long result = jedis.lpush("list", "number 1");
		System.out.println("push result: "+result);
	}
	
	@Test
	public void lpushx(){
		//no operation will be performed when key does not yet exist.
		Long result = jedis.lpushx("list", "numberx 1");
		System.out.println("pushx result: "+ result);
	}
	
	@Test
	public void lpop(){
		//pop item from left of list
		//return item
		String result = jedis.lpop("list");
		System.out.println("pop result: "+result);
	}
	
	@Test
	public void rpush(){
		//push value to the right of list
		//return index of value
		Long result = jedis.rpush("list", "number 2");
		System.out.println("right push :"+result);
	}
	
	@Test
	public void rpushx(){
		//push value to the right of list
		//return index of value
		Long result = jedis.rpushx("list", "numberx 2");
		System.out.println("right pushx :"+result);
	}
	
	@Test
	public void rpop(){
		//pop item from right of list
		//return item
		String result = jedis.rpop("list");
		System.out.println("right pop: "+ result);
	}
	
	@Test
	public void lindex(){
		// start from 0
		String value = jedis.lindex("list", 0);
		System.out.println("value: "+value);
	}
	
	@Test
	public void linsert(){
		//insert value before or after the first found pivot from left
		//if pivot don't exists,return -1
		//if pivot is the first index and insert position is before, just like lpush
		//if pivot is the last index and insert position is after ,just like rpush
		//return the length of list
		Long insertResult = jedis.linsert("list", LIST_POSITION.AFTER, "number 5", "number 3");
		System.out.println("insert result: "+ insertResult);
	}
	
	@Test
	public void lrem(){
		//remove specified count of specified value 
		//if count = 0 ,remove all elements
		//if count is negative,remove from tail to head
		Long result = jedis.lrem("list", -1, "number 3");
		System.out.println("remove result: "+result);
	}
	
	@Test
	public void lset(){
		//set new value at the index position of list
		//if index is out of range , it will generate an error
		//if index is a negative , will set from tail to head
		//return the set result status,if success,return OK
		//if key is not exist,tip no such key
		String result = jedis.lset("list", 10, "new number 2");
		System.out.println("set result: "+result);
	}
	
	@Test
	public void ltrim(){
		//sublist from start to end of ordinary list to modify list stored
		//if start and end is negatives,sub from tail to head
		//if start < 0 or start > end,list will be empty
		//if end > length of list,regarded as the last index
		//useage:list.lpush(value);list.ltrim(list,0,99)  to ensure the space limited
		String result = jedis.ltrim("list",1,jedis.llen("list"));
		System.out.println("trim result: "+result);
	}
	
}
